//
//  MusicVC.m
//  OpenEyes
//
//  Created by dllo on 15/9/9.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MusicVC.h"
#import "MusicViewCell.h"
#import "MusicModel.h"
#import "SecController.h"
#import "SaveViewController.h"

@interface MusicVC ()<requestDelegate>

{
    NSInteger  _count;
}

@end

@implementation MusicVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化，给出空间;
    self.SourArray=[[NSMutableArray alloc]init];
    self.title = @"音乐";
    [self setview];
    [self GetDate];
    _count=1;
    // Do any additional setup after loading the view.
    
//     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"fanhui-3.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(left)];
//    
}
//-(void)left{
//    
//    SaveViewController *save =[[SaveViewController alloc]init];
//    [self.navigationController pushViewController:save animated:YES];
//
//}

//创建页面
-(void)setview{
    //创建tableview
    
    self.tableview=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    [self.tableview setDataSource:self];
    
    [self.tableview setDelegate:self];
    
    [self.tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [self.view addSubview:self.tableview];
    
    //菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.removeFromSuperViewOnHide = YES;
    self.HUD.labelText = @"loading.....";
//    self.HUD.backgroundColor=[UIColor cyanColor];
    [self.view addSubview:self.HUD];
    self.HUD.mode = MBProgressHUDModeIndeterminate;

 [self.tableview addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(action)];
    [self.tableview addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(up)];
}
-(void)up{
    [self.tableview reloadData];
    [self GetDate];
    [self.tableview.header endRefreshing];
    
}
-(void)action{
    [self.tableview reloadData];
    [self GetDate];
    [self.tableview.footer endRefreshing];
}
#pragma uitableview 代理方法


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.SourArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 350;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellId  = @"cell";
    
    MusicViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == NO) {
        cell = [[MusicViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    cell.selectionStyle=UITableViewCellAccessoryNone;
    MusicModel *mu=self.SourArray[indexPath.row];
    [cell add:mu];
    
    return cell;
}


-(void)GetDate{
    
    _count++;
    
    [[HttpRequest httpRequestHandle]loadRequestWithUrl:@"http://lab.daigj.com/api/ios/hm/list" method:@"POST" pramar:[NSString stringWithFormat:@"version=1.0.2&pageNo=%ld&pageSize=10&token=ARk6MCqumCVjvSM1yEuacwOqi%%2FYAFLJxeKE%%3D&category=%%E9%%9F%%B3%%E4%%B9%%90",_count]];
    [HttpRequest httpRequestHandle].delegate= self;

}

    

-(void)requestData:(id)data{
    
    NSMutableArray * listArray=data[@"data"][@"data"][@"list"];
    
    for (NSMutableDictionary * dic in listArray) {
        MusicModel * mm = [[MusicModel alloc] init];
        
        [mm setValuesForKeysWithDictionary:dic];
        mm._id = [dic objectForKey:@"id"];
        [self.SourArray addObject:mm];

        
    }
     [self.HUD setHidden:YES];
    [self.tableview reloadData];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecController * sec =[[SecController alloc]init];
    sec.mo =[self.SourArray objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:sec animated:YES];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
